package cn.itcast.service;

import cn.itcast.dao.CheckItemMapper;
import cn.itcast.domain.CheckItem;
import cn.itcast.entity.PageResult;
import cn.itcast.entity.QueryPageBean;
import com.alibaba.dubbo.config.annotation.Service;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

//使用dubbo的service
@Service(interfaceClass = CheckItemService.class)
//事务控制
@Transactional
public class CheckItemServiceImpl implements CheckItemService {

    //远程调用服务

    @Autowired
    private CheckItemMapper checkItemMapper;


    /**
     * 新增检查项
     *
     * @param checkItem
     */
    @Override
    public void insertCheckItem(CheckItem checkItem) {
        checkItemMapper.insertCheckItem(checkItem);
    }

    /**
     * 分页查询 条件查询
     *
     * @param queryPageBean
     * @return
     */
    @Override
    public PageResult findCheckItemByPage(QueryPageBean queryPageBean) {

        //健壮性判断
        Integer currentPage = queryPageBean.getCurrentPage();
        if (currentPage == null) {
            currentPage = 1;
        }
        Integer pageSize = queryPageBean.getPageSize();
        if (pageSize == null) {
            pageSize = 10;
        }

        PageHelper.startPage(currentPage, pageSize);
        Page<CheckItem> page = (Page<CheckItem>) checkItemMapper.findCheckItemByPage(queryPageBean.getQueryString());

        return new PageResult(page.getTotal(), page.getResult());
    }

    /**
     * 删除检查项
     *
     * @param id
     */
    @Override
    public void deleteCheckItem(Integer id) {
        //判断是否已经关联 先查询
        //先删除中间表的checkItem
        checkItemMapper.delete_CheckItemId(id);
        //再删除
        checkItemMapper.deleteCheckItem(id);

    }

    /**
     * 单个查询检查项
     *
     * @param id
     * @return
     */
    @Override
    public CheckItem findCheckItemById(Integer id) {

        return checkItemMapper.findCheckItemById(id);
    }

    /**
     * 修改检查项
     *
     * @param checkItem
     */
    @Override
    public void updateCheckItem(CheckItem checkItem) {
        checkItemMapper.updateCheckItem(checkItem);
    }

    @Override
    public List<CheckItem> findAllCheckItems() {
        return checkItemMapper.findAllCheckItems();
    }
}